package cn.dakaqi.services.impl;

import cn.dakaqi.dao.ApproveAddressDao;
import cn.dakaqi.entities.ApproveAddress;
import cn.dakaqi.services.ApproveAddressService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: ApproveAddressServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/9/27 17:16
 * @version: 1.0.0
 */
@Component(value = "approveAddressService")
@Transactional
public class ApproveAddressServiceImpl implements ApproveAddressService
{
    @Autowired
    ApproveAddressDao approveAddressDao;

    @Override
    public void save(ApproveAddress approveAddress) throws ServiceRuntimeException
    {
        approveAddressDao.save(approveAddress);
    }

    @Override
    public ApproveAddress findByBaseAddressId(Long addressId) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("addressId", new SearchFilter("addressId", SearchFilter.Operator.EQ, addressId));
        Specification<ApproveAddress> spec = DynamicSpecifications.bySearchFilter(filters.values(), ApproveAddress.class);
        List<ApproveAddress> list = this.approveAddressDao.findAll(spec,sort);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }
}
